package com.zc.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zc.mapper.AdminMapper;
import com.zc.mapper.StudentsMapper;
import com.zc.pojo.Admin;
import com.zc.pojo.Students;
import com.zc.service.StudentsService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StudentsServiceImpl extends ServiceImpl<StudentsMapper, Students> implements StudentsService {
    @Resource
    private StudentsMapper studentsMapper;
    @Override
    public List<String> getExistingNames(List<String> names) {
        if (CollUtil.isEmpty(names)) {
            return CollUtil.emptyIfNull(names);
        }

        // 使用 MyBatis Plus 的查询构造器
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("name", names);
        queryWrapper.select("DISTINCT name");

        return baseMapper.selectList(queryWrapper)
                .stream()
                .map(Students::getName)
                .collect(Collectors.toList());
    }

    @Override
    public List<String> getExistingStudentNumbers(List<String> studentNumbers) {
        if (CollUtil.isEmpty(studentNumbers)) {
            return CollUtil.emptyIfNull(studentNumbers);
        }

        // 使用 MyBatis Plus 的查询构造器
        QueryWrapper<Students> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("student_number", studentNumbers);
        queryWrapper.select("DISTINCT student_number");

        return baseMapper.selectList(queryWrapper)
                .stream()
                .map(Students::getStudentNumber)
                .collect(Collectors.toList());
    }
    //清除表数据
    @Override
    public void deleteAllData() {
        studentsMapper.truncateTable();
    }

    @Override
    public Students getByName(String studentName) {
        LambdaQueryWrapper<Students> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Students::getName, studentName);
        return baseMapper.selectOne(lqw);
    }
}

